﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Odbc;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;


public partial class V2Controls_SpilBarometer : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        int SpilID = int.Parse(Request.QueryString["ID"]);
        OdbcDataReader MyReader = ReviewerOdbc.Read("SELECT V2_Spil.Titel, V2_Spil.SpilID, V2_Spil.ID, V2_GloGenres.Navn FROM  `V2_Spil` JOIN V2_GloGenres ON V2_GloGenres.ID = V2_Spil.Genre WHERE V2_Spil.ID = '" + SpilID + "'");
        MyReader.Read();
        
        //Tomt Billed
        Bitmap MyBitmap = new Bitmap(600, 300);
        //Elementer
        Graphics MyGfx = Graphics.FromImage(MyBitmap);
        Pen MyPen = new Pen(Color.Black);
        Brush MyBrush = new SolidBrush(Color.White);
        Brush MyBrush2 = new SolidBrush(Color.Black);
        Rectangle MyRec = new Rectangle(0, 0, 599, 299);
        Font MyFont = new Font("Verdana", 10);
        //Tegn Baggrund
        MyGfx.FillRectangle(MyBrush, MyRec);
        MyGfx.DrawRectangle(MyPen, 0, 0, 600, 300);
        MyGfx.DrawString(MyReader[0].ToString(), MyFont, MyBrush2, 5, 5);

        //Niveauer
        int[] Cases = new int[5] { 100, 50, 0, 50, 100 };

        int Xtext = 260;

        Pen MyPenSplitLines = new Pen(Color.LightGray);

        foreach (int item in Cases)
        {
            MyGfx.DrawString(item.ToString(), MyFont, MyBrush2, 20, Xtext);
            MyGfx.DrawLine(MyPenSplitLines, 15, Xtext, 590, Xtext);
            Xtext = (Xtext - 58);
        }

        //Tegn x og y ramme
        MyGfx.DrawLine(MyPen, 50, 25, 50, 280);
        MyGfx.DrawLine(MyPen, 15, 260, 590, 260);
        /*
        //Kald til funktionene der tegner linjerne.
        DrawLines(SQL, Color.DarkBlue, MyBitmap, true);
        DrawLines(SQL1, Color.DarkRed, MyBitmap, false);
        DrawLines(SQL2, Color.DarkOrange, MyBitmap, false);
        */

        DateTime StartDate = DateTime.Now.AddDays(-14);
        

        int StartPrik = 80; 
        int Counter = 0;

        float iAVG = 0;
        float iSUM = 0;
        while(Counter != 14)
        {
            OdbcDataReader AVG = ReviewerOdbc.Read("SELECT AVG( Views ) AS VIS FROM V2_SpilViews WHERE Dato = '" + StartDate.AddDays(Counter).ToShortDateString() + "'");
            if (AVG.Read())
            {
                try
                {
                    iAVG = float.Parse(AVG[0].ToString());
                }
                catch
                {
                    iAVG = float.Parse("0.0");
                }
            }
            OdbcDataReader SUM = ReviewerOdbc.Read("SELECT Views FROM V2_SpilViews WHERE Dato = '" + StartDate.AddDays(Counter).ToShortDateString() + "' AND SpilID = " + SpilID);
            if (SUM.Read())
            {
                iSUM = int.Parse(SUM[0].ToString());
            }
            string Dag = StartDate.AddDays(Counter).Day.ToString() + "/" + StartDate.AddDays(Counter).Month.ToString();
            DrawBar(StartPrik, MyBitmap, Dag, iAVG, iSUM);
            Counter++;
            StartPrik = StartPrik + 38;

        }

        MyBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
        Response.ContentType = "image/jpeg";
    }

    public void DrawBar(int Start, Bitmap MyBitmap, string Dag, float AVG, float SUM)
    {
        Color LineColor;
        
        float SIZE = 0;
        float BASE = 145;
        if (AVG > SUM)
        {
            LineColor = Color.Blue;
            SIZE = AVG / SUM * 100;
            SIZE = SIZE / BASE * 100;
            BASE = BASE + SIZE;

            if (BASE > 260)
            {
                BASE = 260;
            }
        }
        else
        {
            LineColor = Color.Red;
            SIZE = AVG / SUM * 100;
            SIZE = SIZE / BASE * 100;
            BASE = BASE - SIZE;

            if (BASE < 20)
            {
                BASE = 20;
            }
        }

        Graphics MyGfx = Graphics.FromImage(MyBitmap);
        Pen MyPen2 = new Pen(LineColor, 20);
        Brush MyBrush2 = new SolidBrush(Color.Black);
        Font MyFont = new Font("Verdana", 8);

        int Xcontent = 260;
        int ycontent = 560;
        int LineStartX = 0;
        int LineStartY = 0;

        MyGfx.DrawLine(MyPen2, Start, 145, Start, BASE);
        MyGfx.DrawString(Dag, MyFont, MyBrush2, Start - 20, 260);
        //MyGfx.DrawString("TEST", MyFont, MyBrush2, 150, Start); 
    }
}
